﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceManager.Utils;
using System.Diagnostics.Contracts;

namespace QuotationService.RealtimeQuotation
{
    internal class BasicInfoRepository<TBasicInfo, TBasicInfoEntity>
        where TBasicInfo : Quotation.BasicInfoBase
        where TBasicInfoEntity : struct
    {
        protected IEntityConverter<TBasicInfo, TBasicInfoEntity> converter;
        public BasicInfoRepository(IEntityConverter<TBasicInfo, TBasicInfoEntity> converter)
        {
            this.converter = converter;
        }

        /// <summary>
        /// 更新单个商品的基本信息
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="basicInfo"></param>
        /// <returns></returns>
        public bool UpdateBasicInfo(string quotSource, TBasicInfo basicInfo)
        {
            Contract.Requires(!string.IsNullOrEmpty(quotSource));
            Contract.Requires(basicInfo != null);
            TBasicInfoEntity basicInfoEntity = this.converter.Convert(basicInfo);
            byte[] bytes = basicInfoEntity.StructToBytes<TBasicInfoEntity>();
            return BasicInfoChannel.Update(quotSource, basicInfo.StkId.LongCode, bytes);
        }

        /// <summary>
        /// 获取单个商品的基本信息
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public TBasicInfo GetBasicInfo(string quotSource, string code)
        {
            Contract.Requires(!string.IsNullOrEmpty(quotSource));
            Contract.Requires(!string.IsNullOrEmpty(code));
            var bytes = BasicInfoChannel.Get(quotSource, code);
            if (bytes != null && bytes.Length > 0)
            {
                TBasicInfoEntity basicInfoEntity = StructHelper.BytesToStruct<TBasicInfoEntity>(bytes);
                return this.converter.Convert(basicInfoEntity);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获取全部商品的基本信息
        /// </summary>
        /// <param name="quotSource"></param>
        /// <returns></returns>
        public List<TBasicInfo> GetAllBasicInfo(string quotSource)
        {
            Contract.Requires(!string.IsNullOrEmpty(quotSource));
            var allBasicInfoBytes = BasicInfoChannel.GetAll(quotSource);
            List<TBasicInfo> rtn = new List<TBasicInfo>();
            if (allBasicInfoBytes != null)
            {
                foreach (byte[] bytes in allBasicInfoBytes)
                {
                    TBasicInfoEntity basicInfoEntity = StructHelper.BytesToStruct<TBasicInfoEntity>(bytes);
                    rtn.Add(this.converter.Convert(basicInfoEntity));
                }
            }
            return rtn;
        }


        /// <summary>
        /// 清除单个商品的基本信息
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public bool ClearBasicInfo(string quotSource, string code)
        {
            Contract.Requires(!string.IsNullOrEmpty(quotSource));
            Contract.Requires(!string.IsNullOrEmpty(code));
            return BasicInfoChannel.Clear(quotSource, code);
        }
        /// <summary>
        /// 清除全部商品的基本信息
        /// </summary>
        /// <param name="quotSource"></param>
        /// <returns></returns>
        public bool ClearAllBasicInfo(string quotSource)
        {
            Contract.Requires(!string.IsNullOrEmpty(quotSource));
            return BasicInfoChannel.Clear(quotSource);
        }
    }


}
